package com.example.hello.controller;

import com.example.hello.pojo.CommentRequest;
import com.example.hello.pojo.CommentResponse;
import com.example.hello.pojo.Res;
import com.example.hello.service.CommunityCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comments")
public class CommunityCommentController {

    @Autowired
    private CommunityCommentService service;

    /** 分页查询根评论及其回复 */
    @GetMapping
    public Res list(
        @RequestParam(defaultValue = "0") int page,
        @RequestParam(defaultValue = "10") int size
    ) {
        List<CommentResponse> data = service.listComments(page, size);
        return Res.success(data);
    }

    /** 发布评论或回复 */
    @PostMapping
    public Res add(@RequestBody CommentRequest req) {
        // TODO: 校验 req.userId, req.content 非空
        CommentResponse cr = service.addComment(req);
        return Res.success(cr);
    }

    /** 删除一条评论 */
    @DeleteMapping("/{id}")
    public Res delete(@PathVariable Long id) {
        service.deleteComment(id);
        return Res.success();
    }
}